package com.action;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.dao.AdministratorDao;
import com.model.Administrator;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends ActionSupport implements ModelDriven<Administrator>{
	private Administrator admin = new Administrator();
	public Administrator getModel() {
		return admin;
	}

	public String login(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		Administrator a = new AdministratorDao().login(admin);
		if (a != null){
			session.put("admin", a);
			return "login";
		}else{
			return "fault";
		}
		
	}
	public String logout(){
		HttpServletResponse response = ServletActionContext.getResponse();
		ActionContext.getContext().getSession().remove("admin");
		try {
			response.getWriter().print("<script>");
			response.getWriter().print("self.top.location.href='./login.jsp'");
			response.getWriter().print("</script>");
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
}
